package com.zc.auth.security.core.access.meta;

import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.web.FilterInvocation;
import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource;

import java.util.Collection;
import java.util.Map;

/**
 * 元数据处理 servlet过滤器使用
 *
 * @author zcj
 * @version 1.0.0
 * @date 2022/1/12 10:22
 */
public class UrlInvocationSecurityMetadataSource implements FilterInvocationSecurityMetadataSource {

    private UrlRoleMetaDataManager urlRoleMetaDataManager;

    public UrlInvocationSecurityMetadataSource() {

    }

    public UrlInvocationSecurityMetadataSource(UrlRoleMetaDataManager urlRoleMetaDataManager) {
        this.urlRoleMetaDataManager = urlRoleMetaDataManager;
    }

    public void setUrlRoleMetaDataManager(UrlRoleMetaDataManager urlRoleMetaDataManager) {
        this.urlRoleMetaDataManager = urlRoleMetaDataManager;
    }

    @Override
    public Collection<ConfigAttribute> getAttributes(Object obj) throws IllegalArgumentException {
        FilterInvocation fi = (FilterInvocation) obj;
        String url = fi.getRequestUrl();
        // 获取能访问url的角色
        Map<String, ? extends Collection<String>> map = urlRoleMetaDataManager.getUrlsAndRolesMap();
        return urlRoleMetaDataManager.getConfigAttributes(url, map);
    }

    @Override
    public Collection<ConfigAttribute> getAllConfigAttributes() {
        return urlRoleMetaDataManager.getAllConfigAttributes();
    }

    @Override
    public boolean supports(Class<?> aClass) {
        return true;
    }
}
